package com.xywl.escort.view.order.subscription;

import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.RepurchaseReq;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class RepurchaseDialogPresenter implements RepurchaseDialogContract.Presenter {
    private RepurchaseDialogContract.View mView;
    private ApiServer mApiServer;

    @Inject
    RepurchaseDialogPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(RepurchaseDialogContract.View view) {
        this.mView = view;
    }

    @Override
    public void repurchase(String projectId, int num) {
        RepurchaseReq req = new RepurchaseReq();
        req.projectId = projectId;
        req.num = num;
        mApiServer.repurchaseApply(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.repurchaseFailure("操作失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.repurchaseSuccess(resp);
                            } else {
                                mView.repurchaseFailure(resp.msg);
                            }
                        } else {
                            mView.repurchaseFailure("操作失败");
                        }
                    }
                });
    }


    @Override
    public void unSubscribe() {
        Timber.tag("RepurchaseDialogContract").e("unSubscribe");
        mView = null;
    }
}
